package com.grgbanking.it.system.controller;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.grgbanking.it.common.entity.PageQuery;
import com.grgbanking.it.common.json.JSONHelper;
import com.grgbanking.it.system.entity.User;
import com.grgbanking.it.system.service.IUserService;

/**
 * OA的数据源中获取的信息
 * 包括:部门、用户、资产
 * @author pgwei
 *
 */
@Controller
@RequestMapping("/system")
public class UserController {
	
	@Autowired
	private IUserService userService;
	
	/**
	 * 登录
	 * login
	 * 
	 * @param session
	 * @param user
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("user/login")
	public ModelAndView login(HttpSession session,User user){
		ModelAndView view = new ModelAndView();
		try {
			User loginUser = userService.login(user);
			if (loginUser == null) {
				view.setViewName("redirect:/logout.jsp?userError=true");
			} else {
				view.setViewName("redirect:/main.jsp");
				session.setAttribute("loginUser", loginUser);
				session.setMaxInactiveInterval(3 * 60 * 60);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return view; 
	}
	
	/**
	 * 加载用户数据
	 * 
	 * @param name
	 * @return
	 */
	@ResponseBody  
	@RequestMapping(value="user",method=RequestMethod.GET)
    public Object loadUsers(String keyword,String departmentId,PageQuery query){  
		String json = null;
        try {
			json = userService.findUsersToJson(query,keyword,departmentId);
		} catch (Exception e) {
			e.printStackTrace();
		}  
        return json;  
    }   
	
	/**
	 * 为用户分配角色
	 * distributeRoles
	 * 
	 * @param id
	 * @param resourceIds
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody  
	@RequestMapping(value="user/role",method=RequestMethod.POST)
    public Object distributeRoles(String userId,String roleIds){  
		String json = null;
        try {
        	userService.updateDistributeRoles(userId, roleIds);
        	json = JSONHelper.getSuccessMessage("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			json = JSONHelper.getExceptionMessage(e);
		}  
        return json;  
    }
	
	/**
	 * 导出所有的微信用户
	 * 
	 * @param name
	 * @return
	 */
	@RequestMapping(value="user/wx",method=RequestMethod.GET)
    public void exportUsersFromWX(HttpServletResponse response){  
        try {
			HSSFWorkbook book = userService.exportUserFromWX();
			// 组装附件名称和格式
			response.setHeader("Content-disposition", "attachment;filename=WeChat.xlsx");  
			book.write(response.getOutputStream());
		} catch (Exception e) {
			e.printStackTrace();
		}  
    }   
}
